package com.swk.loadResources;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ServletContext加载资源文件
 */
@WebServlet("/LoadBySC")
public class LoadBySC extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public LoadBySC() {
        super();
    }

	/**
	 * 路径是相对于web服务器的当前web应用目录而言，此时/表示:当前web应用根目录
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		ServletContext context = this.getServletContext();
		//文件在IDE工具的src目录下
//		InputStream is = context.getResourceAsStream("/WEB-INF/classes/config1.properties");
//		InputStream is = context.getResourceAsStream("/WEB-INF/classes/com/swk/config/config2.properties");
		//文件在IDE工具的WebContent目录下
		InputStream is = context.getResourceAsStream("/config3.properties");
		Properties prop = new Properties();
		prop.load(is);
		System.out.println(prop.getProperty("name"));
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
